#include <bits/stdc++.h>

using namespace std;

int s[10005];
int height[10005] = {0};

int count1 = 0;

struct Node
{
    int x, y;
    int ti;
};

struct mycompare{
    bool operator()(Node A, Node B)
    {
        return A.ti > B.ti;
    } 
};

int find_root(int x)
{
    if(s[x] != x)s[x] = find_root(s[x]);

    return s[x];
}

void merge_root(int x, int y)
{
    x = find_root(x);
    y = find_root(y);

    if(x != y)
    {
        s[x] = s[y];
        count1 += 1;
    }
}

int main()
{
    int M, N;
    cin>>N>>M;

    priority_queue<Node,vector<Node>,mycompare> q;

    for(int i = 1; i <= N; i++)
        s[i] = i;

    for(int i = 0; i < M; i++)
    {
        Node temp;
        cin >> temp.x>>temp.y>>temp.ti;
        q.push(temp);
    }

    bool judge = false;
    while(!q.empty())
    {
        Node temp;
        temp = q.top();
        q.pop();
        merge_root(temp.x, temp.y);
        if(count1 == N-1)
        {    cout << temp.ti;
            judge = true;
            break;
        }
    }

    if(!judge)cout<<(-1);

    return 0;
}